package com.lxyk.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxyk.yygh.common.result.Result;
import com.lxyk.yygh.common.utils.MD5;
import com.lxyk.yygh.hosp.service.HospitalSetService;
import com.lxyk.yygh.model.hosp.HospitalSet;
import com.lxyk.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;

/**
 * 医院设置表
 */
//@CrossOrigin
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息

    /**
     *  查询医院设置表所有信息
     * @return
     */
    @ApiOperation("查询医院设置表所有信息")
    @GetMapping("/findAll")
    public Result getAllInfo()
    {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2 逻辑删除医院设置

    /**
     * 逻辑删除医院设置
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("/removeById/{id}")
    public Result  removeById(@PathVariable int id){
        boolean b = hospitalSetService.removeById(id);
        return Result.ok(b);

    }

    //3 条件查询带分页

    /**
     *
     * @param current  当前页
     * @param limit    每一页的记录
     * @param hospitalSetQueryVo  医院编号和医院名字封装的是
     * @return
     */
    @PostMapping("/pageCondition/{current}/{limit}")
    @ApiOperation("条件查询带分页查询医院设置表")
    public Result pageCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
//            条件查询带分页
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(hospitalSetQueryVo.getHoscode()!=null,"hoscode",hospitalSetQueryVo.getHoscode());
        queryWrapper.eq(hospitalSetQueryVo.getHosname()!=null,"hosname",hospitalSetQueryVo.getHosname());
        hospitalSetPage= hospitalSetService.page(hospitalSetPage, queryWrapper);
        return Result.ok(hospitalSetPage);


//        Page<HospitalSet> page = new Page<>(current, limit);
////        条件封装
//        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
//        wrapper.eq(hospitalSetQueryVo.getHoscode() != null, "hoscode", hospitalSetQueryVo.getHoscode());
//        wrapper.like(hospitalSetQueryVo.getHosname()!=null,"hosname",hospitalSetQueryVo.getHosname());
////        查询
//        page=hospitalSetService.page(page,wrapper);
//        return Result.ok(page);
    }

    //4 添加医院设置1
    @PostMapping("/addHospitalSet")
    @ApiOperation("添加医院设置")
    public Result addHospitalSet(@RequestBody HospitalSet hospitalSet){
        if(hospitalSet!=null){
            //设置状态,1使用，，0未使用
            hospitalSet.setStatus(1);
            //生成签名密钥，使用MD5加密
            Random random = new Random();
            //使用当前系统时间+随机数就不会重复了
            String signKey= MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000));
            hospitalSet.setSignKey(signKey);
            boolean b = hospitalSetService.save(hospitalSet);
            if(b){
                return Result.ok();
            }
        }
        return Result.fail();
    }

    //5根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospitalSetById/{id}")
    public Result getHospitalSetById(@PathVariable Integer id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospital")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //7.批量删除医院设置

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("/deleteBatchHospitalSet")
    public Result deleteBatchHospitalSet(@RequestBody List<Long> idList){

        hospitalSetService.removeByIds(idList);
        return Result.ok();

    }

    //8.医院设置锁定和解锁,修改
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("HospitalSetStatus/{id}/{status}")
    public Result HospitalSetStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(!flag){

            return Result.fail();
        }
        return Result.ok();
    }

    //9.发送签名密钥
    @ApiOperation("发送签名密钥")
    @GetMapping("/sendSignKey/{id}")
    public Result sendSignKey(@PathVariable Long id){
        //根据id获取医院设置详细信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // 通过医院的接口 和我们的 平台对接  双方要约定好一个秘钥  发送秘钥到医院的接口进行配置  这里就是做一个短信的发送 （后面统一完善）
        //todo 使用短信发送 2023/6/18/
        return Result.ok();
    }
}
